package com.sxc.OnlineStore.Utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectMySql {
    static Properties p = null;
    static Connection con = null;

    //要把加载配置文件的信息写在静态代码块里面：程序执行的时候，配置文件只需要加载一次
    static{
        try {//加载配置文件
            InputStream in = ConnectMySql.class.getClassLoader().getResourceAsStream("ConnectInfo.properties");

            p = new Properties();
            p.load(in);		//将输入流的信息加载到p对象里面
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Connection conMysql() {
        try {
            Class.forName( p.getProperty("driverClass") );
            con = DriverManager.getConnection(p.getProperty("url"), p.getProperty("username"), p.getProperty("password"));

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException err) {
            // TODO Auto-generated catch block
            err.printStackTrace();
        }
        return con;
    }

    public Connection getConnection(){
            return con;

    }

}
